/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.model.content {
	import com.googlecode.milib.base.IChangable;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.service.singleton.EnterFrameService;

	import flash.utils.getTimer;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGSpeedTesterTest extends MLBroadcastClass implements IChangable {
	
		public var name:String;
		private var startTime:int;
		private var lastTime:int;
		private var maxTime:Number=0;
		private var minTime:Number=Number.MAX_VALUE;
		private var efs:EnterFrameService;
		private var lastFrameTime:Number;
		private var currentFrameNum:Number=0;
		private var in1FrameTime:Number;
		private var in1FrameCount:Number;
		private var lastIn1FrameTime:Number=0;

		
		public function MLDBGSpeedTesterTest(name:String) {
			this.name=name;
			efs=EnterFrameService.getInstance();
		}
		
		public function get event_Changed():String { return 'Changed'; }
		
		public function getResult():String {
			if(in1FrameCount){
				return 'lastTime:<b>'+lastTime+'</b> in1FrameTime:<b>'+in1FrameTime+'</b> in1FrameCount:<b>'+in1FrameCount+'</b> max:<b>'+maxTime+'</b>, min:<b>'+minTime+'</b> toltal time:<b>'+Math.round((lastIn1FrameTime/lastFrameTime)*100)+'%</b>';
			}else{
				return '(no finish)';
			}
		}
		
		public function setStartTime():void {
			if(currentFrameNum!=efs.getFrameNumber()){
				currentFrameNum=efs.getFrameNumber();
				lastIn1FrameTime=in1FrameTime;
				in1FrameTime=0;				in1FrameCount=0;
			}
			startTime=getTimer();
		}
		
		public function setFinishTime():void {
			in1FrameCount++;
			lastTime=getTimer()-startTime;
			in1FrameTime+=lastTime;
			
			lastFrameTime=efs.getLastFrameTime();
			
			maxTime=Math.max(maxTime, lastTime);
			minTime=Math.min(minTime, lastTime);
		}
		
		
	}
}
